'use strict';

const { Controller } = require('egg');

class CounselorController extends Controller {

    async list() {
        const { ctx, app } = this;
        const pageSize = ctx.query.pageSize ? parseInt(ctx.query.pageSize) : 10;
        const pageNumber = ctx.query.pageNumber ? parseInt(ctx.query.pageNumber) : 1;
        let where = {};
        let { count, rows } = await app.model.Counselor.findAndCountAll({
            where: where,
            offset: (pageNumber - 1) * pageSize,
            limit: pageSize,
        });
        // 返回用户信息和token
        ctx.apiSuccess({
            totalCount: count,
            currPage: pageNumber,
            list: rows
        });
    };


    async addCounselor() {
        const { ctx, app } = this;
        ctx.validate({
            counselorcoverimg: {
                type: 'string',
                required: true,
                desc: "图片路径"
            },
            counselordetailcontent: {
                type: 'string',
                desc: '详细',
                default: ''
            },
            counselorname: {
                type: 'string',
                required: true,
                desc: '课程名称',
                default: ''
            },
            counselorsellstatus: {
                type: 'int',
                required: true,
                desc: '状态',
                range: { in: [0, 1]
                }
            },
            originalprice: {
                type: 'int',
                required: true,
                desc: '课程价格',
            },
            counselortype: {
                type: 'string',
                required: true,
                desc: '课程类型',
            },
            counselorimg: {
                type: 'string',
                required: true,
                desc: "图片路径"
            },
            counselorsellstatus: {
                type: 'int',
                required: true,
                desc: '推荐',
                range: { in: [0, 1]
                }
            }
        });
        const { counselorcoverimg, counselordetailcontent, counselorname, counselorsellstatus, originalprice, counselortype, counselorimg, counselorrecommend, counselorlecturer } = ctx.request.body;
        await app.model.Counselor.create({
            counselorcoverimg,
            counselordetailcontent,
            counselorname,
            counselorsellstatus,
            originalprice,
            counselortype,
            counselorimg,
            counselorrecommend,
            counselorlecturer
        });
        ctx.apiSuccess('添加成功')
    };

    async updateCounselor() {
        const { ctx, app } = this;
        ctx.validate({
            id: {
                type: 'int',
                required: true,
                desc: '轮播图id',
            },
            counselorcoverimg: {
                type: 'string',
                required: true,
                desc: "图片路径"
            },
            counselordetailcontent: {
                type: 'string',
                desc: '详细',
                default: ''
            },
            counselorname: {
                type: 'string',
                required: true,
                desc: '课程名称',
                default: ''
            },
            counselorsellstatus: {
                type: 'int',
                required: true,
                desc: '状态',
                range: { in: [0, 1]
                }
            },
            originalprice: {
                type: 'int',
                required: true,
                desc: '课程价格',
            },
            counselortype: {
                type: 'string',
                required: true,
                desc: '课程类型'
            },
            counselorimg: {
                type: 'string',
                required: true,
                desc: "图片路径"
            },
            counselorrecommend: {
                type: 'int',
                required: true,
                desc: '推荐',
                range: { in: [0, 1]
                }
            }
        });
        const { counselorcoverimg, counselordetailcontent, counselorname, counselorsellstatus, originalprice, sellingprice, counselortype, counselorimg, counselorrecommend, counselorlecturer, id } = ctx.request.body;
        let Counselor = await app.model.Counselor.findOne({ id });
        if (!Counselor) {
            ctx.throw('更新失败,不存在此课程')
        };
        // swiper.image = image;
        // swiper.link = link;
        // swiper.order = order;
        // await swiper.save();
        await app.model.Counselor.update({ counselorcoverimg, counselordetailcontent, counselorname, counselorsellstatus, originalprice, sellingprice, counselortype, counselorimg, counselorrecommend, counselorlecturer }, {
            where: {
                id
            }
        })
        ctx.apiSuccess('更新成功');
    }

    async deleteCounselor() {
        const { ctx, app } = this;
        ctx.validate({
            ids: {
                type: 'all',
                desc: '课程ids'
            }
        });
        let { ids } = ctx.request.body;
        console.log(JSON.parse(ids))
        ids = JSON.parse(ids);
        await app.model.Counselor.destroy({
            where: {
                id: ids
            }
        });
        ctx.apiSuccess('删除成功');
    }

    async detail() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        // console.log(id)
        let Counselor = await app.model.Counselor.findOne({ where: { id } });
        // console.log(Counselor)
        ctx.apiSuccess(Counselor);
    }
}

module.exports = CounselorController;